package com.ruoyi.web.controller.business.rule;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.business.base.PageParam;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.controller.business.rule.vo.JbRulesHistoryVO;
import com.ruoyi.web.core.JoController;
import com.ruoyi.web.core.tool.ColumnUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.rule.domain.JbRulesHistory;
import com.ruoyi.business.rule.service.JbRulesHistoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 业务规则历史 Controller
 *
 * @author ruoyi
 * @date 2024-10-11
 */
@Api("业务规则历史")
@RestController
@RequestMapping("/rules_history")
public class JbRulesHistoryController extends JoController
{
    @Autowired
    private JbRulesHistoryService service;

    /**
     * 查询业务规则历史列表
     */
    @SneakyThrows
    @ApiOperation("查询业务规则历史列表")
    @GetMapping("/list")
    public TableDataInfo list(PageParam page, JbRulesHistory jbRulesHistory)
    {
        LambdaQueryWrapper<JbRulesHistory> select = new LambdaQueryWrapper<>();
        select.orderByDesc(JbRulesHistory::getCreateTime);
        select.eq(StringUtils.isNotEmpty(jbRulesHistory.getRulesTypeCode()), JbRulesHistory::getRulesTypeCode,
                jbRulesHistory.getRulesTypeCode());

        IPage<JbRulesHistory> iPage = service.page(page.b(), select);

        List<JbRulesHistory> list = iPage.getRecords();
        List<JbRulesHistoryVO> voList = BeanUtil.copyToList(list, JbRulesHistoryVO.class);

        //获取数据集
        List<Long> idList = ids(voList, ColumnUtil.F(JbRulesHistoryVO::getCreateBy));
        List<SysUser> users = userController.listByIds(idList);

        //规则
        Map map = kv(
                ColumnUtil.F(SysUser::getUserId),
                ColumnUtil.F(SysUser::getNickName),
                users
        );
        toKey(voList, map,
                ColumnUtil.F(JbRulesHistoryVO::getCreateBy),
                ColumnUtil.F(JbRulesHistoryVO::getUserName)
        );

        return getDataTable(voList, iPage.getTotal());
    }

    /**
     * 导出业务规则历史列表
     */
    @ApiOperation("导出业务规则历史列表")
    @Log(title = "业务规则历史", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PageParam page, JbRulesHistory jbRulesHistory)
    {
        LambdaQueryWrapper<JbRulesHistory> select = new LambdaQueryWrapper<>();
        IPage<JbRulesHistory> iPage = service.page(page.b(), select);

        List<JbRulesHistory> list = iPage.getRecords();
        ExcelUtil<JbRulesHistory> util = new ExcelUtil<JbRulesHistory>(JbRulesHistory.class);
        util.exportExcel(response, list, "业务规则历史数据");
    }

    /**
     * 获取业务规则历史详细信息
     */
    @ApiOperation("获取业务规则历史详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(service.getById(id));
    }

    /**
     * 新增业务规则历史
     */
    @ApiOperation("新增业务规则历史")
    @Log(title = "业务规则历史", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JbRulesHistory jbRulesHistory)
    {
        return toAjax(service.save(jbRulesHistory));
    }

    /**
     * 修改业务规则历史
     */
    @ApiOperation("修改业务规则历史")
    @Log(title = "业务规则历史", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JbRulesHistory jbRulesHistory)
    {
        return toAjax(service.updateById(jbRulesHistory));
    }

    /**
     * 删除业务规则历史
     */
    @ApiOperation("删除业务规则历史")
    @Log(title = "业务规则历史", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(service.removeByIds(Arrays.asList(ids)));
    }
}
